package com.example.teacherdemo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class DateTimeConfig {

    // 定义统一的时间格式
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule module = new JavaTimeModule();
        
        // 配置LocalDateTime的反序列化器（前端→后端）
        LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(
            DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)
        );
        // 配置LocalDateTime的序列化器（后端→前端）
        LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(
            DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)
        );
        
        module.addDeserializer(LocalDateTime.class, deserializer);
        module.addSerializer(LocalDateTime.class, serializer);
        
        objectMapper.registerModule(module);
        return objectMapper;
    }
}